// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include <windows.h>
#include <utility>

#include "sdk_engine/adll.h"
#include "GameManager.h"
#include "InputManager.h"
#include "Player.h"
#include "TopPanel.h"
#include "MediaManager.h"

using namespace std;

TopPanel::TopPanel(void)
	: ImagePanel(make_pair(32, 16), 1, "top_frame.png"),
	_quitButton(make_pair(64, 16), 2, "quit.png"),
	_newButton(make_pair(144, 16), 2, "new.png"),
	_saveButton(make_pair(224, 16), 2, "save.png"),
	_gold(_VAR(0)), _time(_VAR(0))
{
	//income
	_panelDefinition += "digits(380, 28, \"%3.0f\", *, 1, 0);\n";
	//time
	_panelDefinition += "digits(600, 28, \"%3.2f days\", *, 1, 1);\n";

	InputManager* im = GameManager::getInstance()->getInputManager();

	//register buttons with the input manager
	im->registerButton(&_quitButton);
	im->registerButton(&_newButton);
	im->registerButton(&_saveButton);

	_quitButton.addObserver(this);
	_newButton.addObserver(this);
	_saveButton.addObserver(this);

	_quitButton.setToolTipText("Goodbye world");
	_newButton.setToolTipText("New game");
	_saveButton.setToolTipText("Save game");
}

TopPanel::~TopPanel(void)
{
}

//create PANEL useable by engine
void TopPanel::createPanel()
{
	ImagePanel::createPanel();
	
	digits_set(_pPanel, _VAR(1), &_gold);
	digits_set(_pPanel, _VAR(2), &_time);

	_quitButton.createPanel();
	_newButton.createPanel();
	_saveButton.createPanel();
}

void TopPanel::update(float timestep, float totalTime)
{
	GameManager* gm = GameManager::getInstance();
	Player* pl = gm->getPlayers()[0];
	_gold = _VAR(pl->getIncome());
	_time = _VAR(totalTime/DAY_IN_SECONDS);
	
}

// Makes the panel visible
void TopPanel::display()
{
	ImagePanel::display();
	_quitButton.display();
	_newButton.display();
	_saveButton.display();
}

// Hides the panel
void TopPanel::hide()
{
	ImagePanel::hide();
	_quitButton.hide();
	_newButton.hide();
	_saveButton.hide();
}

void TopPanel::on_clicked(Button *clickedButton) 
{
	
	if(clickedButton==&_quitButton) {
		SOUND* s = MediaManager::getInstance()->getSound("3dgsSux.ogg");
		snd_play(s,_VAR(1000),_VAR(0));
		Sleep(2000);
		exit(0);
	}
	else if(clickedButton==&_newButton) {
		
	}
	else if(clickedButton==&_saveButton) {
	
	}
}